package com.hardson.toolkit.swing.jbutton;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JToolTip;

import com.hardson.toolkit.interfaces.ICallbackAction;

public class TrButton extends JButton {

	private static final long serialVersionUID = 1L;

	public TrButton(final String text, final Icon icon, ICallbackAction<ActionEvent> action) {
		super(text, icon);
		this.setBorderPainted(false);//开始时不编制边界
		this.setContentAreaFilled(false);//设置透明
		this.setMargin(new Insets(2, 14, 2, 14));
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(final MouseEvent e) {
				TrButton.this.mouseEntered0(e);
			}

			@Override
			public void mouseExited(final MouseEvent e) {
				TrButton.this.mouseExited0(e);
			}
		});
		if (action != null) {
			this.addActionListener(action::execute);
		}
	}

	protected void mouseEntered0(final MouseEvent e) {
		if (this.isEnabled()) {
			this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			this.setBorderPainted(true);
			this.setContentAreaFilled(true);
			this.repaint();
		}
	}

	protected void mouseExited0(final MouseEvent e) {
		if (this.isEnabled()) {
			this.setCursor(Cursor.getDefaultCursor());
			this.setBorderPainted(false);
			this.setContentAreaFilled(false);
			this.repaint();
		}
	}

	@Override
	public JToolTip createToolTip() {
		JToolTip jt = super.createToolTip();
		jt.setBackground(new Color(255, 255, 220));
		jt.updateUI();
		return jt;
	}
}
